﻿using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// Linkman 的摘要说明。
    /// </summary>
    public class MyLinkman
    {
        #region 得到联系人列表

        /// <summary>
        /// 得到我的联系人
        /// </summary>
        /// <param name="type">联系人类型(0:所有联系人1:员工联系人2:客户联系人3:自定义联系人)</param>
        /// <param name="staffid">用户id</param>
        /// <returns></returns>
        public SqlDataReader GetMyLinkman(int type, int staffid)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@type", SqlDbType.Int, 4, type),
                Database.MakeInParam("@staffid", SqlDbType.Int, 4, staffid)
            };
            return data.RunDataReader("dbo.sproc_Linkman_GetList", parameters);
        }

        #endregion

        #region 得到自定义联系人

        /// <summary>
        /// 得到自定义联系人
        /// </summary>
        /// <returns></returns>
        public SqlDataReader GetCustomLinkman(int id)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@id", SqlDbType.Int, 4, id)
            };
            return data.RunDataReader("dbo.sproc_Linkman_GetCustomLinkman", parameters);
        }

        #endregion

        #region 得到自定义联系人类别

        public SqlDataReader GetCustomLinkmanType(int id)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@id", SqlDbType.Int, 4, id)
            };
            return data.RunDataReader("dbo.sproc_Linkman_GetCustomLinkmanTypeByCustomLinkmanID", parameters);
        }

        #endregion

        #region 获得所有联系人分类关系数据

        /// <summary>
        /// 获得所有联系人分类关系数据
        /// </summary>
        /// <returns></returns>
        public SqlDataReader GetLinkmanTypeRelation()
        {
            var data = new Database();
            return data.RunDataReader("dbo.sproc_Linkman_GetAllLinkmanTypeData");
        }

        #endregion

        #region 添加自定义联系人

        /// <summary>
        /// 添加自定义联系人
        /// </summary>
        /// <param name="clinkman">CustomLinkman结构</param>
        /// <param name="staffid">The id of the staff who created the contact.</param>
        /// <returns>添加的ID</returns>
        public int AddCustomLinkman(CustomLinkman clinkman, int staffid)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@name", SqlDbType.NVarChar, 100, clinkman.Name),
                Database.MakeInParam("@Gender", SqlDbType.Bit, 1, clinkman.Gender),
                Database.MakeInParam("@UnitTelephone", SqlDbType.NVarChar, 50, clinkman.UnitTelephone),
                Database.MakeInParam("@Email", SqlDbType.NVarChar, 50, clinkman.Email),
                Database.MakeInParam("@UnitAddress", SqlDbType.NVarChar, 200, clinkman.UnitAddress),
                Database.MakeInParam("@UnitZip", SqlDbType.NVarChar, 50, clinkman.UnitZip),
                Database.MakeInParam("@FamilyAddress", SqlDbType.NVarChar, 200, clinkman.FamilyAddress),
                Database.MakeInParam("@FamilyZip", SqlDbType.NVarChar, 50, clinkman.FamilyZip),
                Database.MakeInParam("@Mobile", SqlDbType.NVarChar, 100, clinkman.Mobile),
                Database.MakeInParam("@FamilyTelephone", SqlDbType.NVarChar, 100, clinkman.FamilyTelephone),
                Database.MakeInParam("@Memo", SqlDbType.NVarChar, 1000, clinkman.Memo),
                Database.MakeInParam("@Staffid", SqlDbType.Int, 4, staffid),
                Database.MakeInParam("@Age", SqlDbType.NVarChar, 50, clinkman.Age)
            };
            int result = data.RunProc("dbo.sproc_Linkman_AddCustomLinkman", parameters);

            if (clinkman.Type.IndexOf(',', 0) != -1)
            {
                string[] arrtype = clinkman.Type.Split(',');
                for (int i = 0; i < arrtype.Length; i++)
                {
                    if (arrtype[i].Trim() != "")
                    {
                        SqlParameter[] prams1 = {
                            Database.MakeInParam("@typeid", SqlDbType.Int, 4, arrtype[i]),
                            Database.MakeInParam("@id", SqlDbType.Int, 4, result)
                        };
                        data.RunProc("dbo.sproc_Linkman_AddCustomLinkmanType", prams1);
                    }
                }
            }

            return (result);
        }

        #endregion

        #region 更新自定义联系人

        /// <summary>
        /// 更新自定义联系人
        /// </summary>
        /// <param name="clinkman"></param>
        public void UpdateCustomLinkman(CustomLinkman clinkman)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@ID", SqlDbType.Int, 4, clinkman.ID),
                Database.MakeInParam("@name", SqlDbType.NVarChar, 100, clinkman.Name),
                Database.MakeInParam("@Gender", SqlDbType.Bit, 1, clinkman.Gender),
                Database.MakeInParam("@UnitTelephone", SqlDbType.NVarChar, 50, clinkman.UnitTelephone),
                Database.MakeInParam("@Email", SqlDbType.NVarChar, 50, clinkman.Email),
                Database.MakeInParam("@UnitAddress", SqlDbType.NVarChar, 200, clinkman.UnitAddress),
                Database.MakeInParam("@UnitZip", SqlDbType.NVarChar, 50, clinkman.UnitZip),
                Database.MakeInParam("@FamilyAddress", SqlDbType.NVarChar, 200, clinkman.FamilyAddress),
                Database.MakeInParam("@FamilyZip", SqlDbType.NVarChar, 50, clinkman.FamilyZip),
                Database.MakeInParam("@Mobile", SqlDbType.NVarChar, 100, clinkman.Mobile),
                Database.MakeInParam("@FamilyTelephone", SqlDbType.NVarChar, 100, clinkman.FamilyTelephone),
                Database.MakeInParam("@Memo", SqlDbType.NVarChar, 1000, clinkman.Memo),
                Database.MakeInParam("@Age", SqlDbType.NVarChar, 50, clinkman.Age),
                Database.MakeInParam("@Type", SqlDbType.NVarChar, 200, clinkman.Type)
            };
            data.RunProc("dbo.sproc_Linkman_UpdateCustomLinkman", parameters);
        }

        #endregion

        #region 获得自定义联系人分类

        /// <summary>
        /// 获得自定义联系人分类
        /// </summary>
        /// <returns></returns>
        public static SqlDataReader GetCustomLinkmanType()
        {
            var data = new Database();
            return data.RunDataReader("dbo.sproc_Linkman_GetCustomLinkmanType");
        }

        #endregion

        #region 根据分类得到自定义联系人

        /// <summary>
        /// 根据分类得到自定义联系人
        /// </summary>
        /// <param name="type">类别id</param>
        /// <param name="staffid">staffid</param>
        /// <returns></returns>
        public SqlDataReader GetCustomLinkmanByType(int type, int staffid)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@typeid", SqlDbType.Int, 4, type),
                Database.MakeInParam("@staffid", SqlDbType.Int, 4, staffid)
            };
            return data.RunDataReader("dbo.sproc_Linkman_GetCustomLinkmanByType", parameters);
        }

        #endregion

        #region 更新自定义联系人分类

        /// <summary>
        /// 更新自定义联系人分类
        /// </summary>
        /// <param name="clinkman"></param>
        public void UpdateCustomLinkmanType(CustomLinkman clinkman)
        {
            var data = new Database();
            if (clinkman.Type.IndexOf(',', 0) != -1)
            {
                string[] arrtype = clinkman.Type.Split(',');
                for (int i = 0; i < arrtype.Length; i++)
                {
                    if (arrtype[i].Trim() != "")
                    {
                        SqlParameter[] prams1 = {
                            Database.MakeInParam("@typeid", SqlDbType.Int, 4, arrtype[i]),
                            Database.MakeInParam("@id", SqlDbType.Int, 4, clinkman.ID)
                        };
                        data.RunProc("dbo.sproc_Linkman_AddCustomLinkmanType", prams1);
                    }
                }
            }
        }

        #endregion

        #region 添加联系人到联系人列表

        /// <summary>
        /// 添加联系人到联系人列表
        /// </summary>
        /// <param name="type">联系人类别1:员工联系人2:客户联系人3:自定义联系人</param>
        /// <param name="id">联系人id</param>
        /// <param name="staffid">用户id</param>
        public void AddLinkmanToList(int type, int id, int staffid)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@type", SqlDbType.Int, 4, type),
                Database.MakeInParam("@id", SqlDbType.Int, 4, id),
                Database.MakeInParam("@staffid", SqlDbType.Int, 4, staffid)
            };
            db.RunProc("dbo.sproc_Linkman_AddToLinkmanList", parameters);
        }

        #endregion

        #region 删除联系人列表中的联系人

        /// <summary>
        /// 删除联系人列表中的联系人
        /// </summary>
        /// <param name="type">The type of the contacts.</param>
        /// <param name="linkmanid">联系人列表中的id</param>
        public void DelLinkmanFromList(int type, int linkmanid)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@type", SqlDbType.Int, 4, type),
                Database.MakeInParam("@linkmanid", SqlDbType.Int, 4, linkmanid)
            };
            db.RunProc("dbo.sproc_Linkman_DelLinkmanFromList", parameters);
        }

        #endregion

        #region 检查此人是否已经在联系人列表中

        /// <summary>
        /// 检查此人是否已经在联系人列表中
        /// </summary>
        /// <param name="type">联系人类型(0:所有联系人1:员工联系人2:客户联系人3:自定义联系人)</param>
        /// <param name="staffid">用户id</param>
        /// <param name="id">联系人id</param>
        /// <returns></returns>
        public bool HaveInList(int type, int staffid, int id)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@type", SqlDbType.Int, 4, type),
                Database.MakeInParam("@staffid", SqlDbType.Int, 4, staffid),
                Database.MakeInParam("@id", SqlDbType.Int, 4, id)
            };
            return ((data.RunProc("dbo.sproc_Linkman_HaveInList", parameters) == 1) ? true : false);
        }

        #endregion
    }
}